package com.lv.suyiyong.hfr.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lv.common.base.DataBindBaseViewHolder
import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.LoadSrc
import com.lv.common.ktx.applyGrayScale
import com.lv.common.ktx.setBname
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.Constants
import com.lv.service.bean.GoodsItemBean
import com.lv.service.bean.SpuItemBean
import com.lv.service.room.company.CompanyItemBean
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.ItemFactoryBinding
import com.lv.suyiyong.hfr.databinding.ItemFactoryMustBinding


/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class MustFactoryAdapter(
    private val datas: MutableList<CompanyItemBean>,
) :
    BaseQuickAdapter<CompanyItemBean, DataBindBaseViewHolder>
        (R.layout.item_factory_must, datas) {

    override fun convert(holder: DataBindBaseViewHolder, item: CompanyItemBean) {
        val mBinding: ItemFactoryMustBinding =
            holder.getBind() as ItemFactoryMustBinding
//        mBinding.itemRoot = item
//        //防止列表闪烁
        mBinding.executePendingBindings()
        val layoutPosition = holder.layoutPosition
        mBinding.ivGold.LoadSrc(Constants.GOLD_TAG)
        mBinding.ivFactory.LoadSrc(Constants.FACTORY_TAG_F)
        mBinding.ivBrand.LoadSrc(Constants.BRAND_TAG_F)
        val isspecially = item.isspecially
        val factory = item.factory
        val brand = item.brand
        if (isspecially == "2" || factory == "1" || brand == "1") {
            mBinding.llTag.visibility = View.VISIBLE
            mBinding.ivGold.visibility = if (isspecially == "2") View.VISIBLE else View.GONE
//            mBinding.tvRz.visibility = if (isspecially == "1") View.VISIBLE else View.GONE
            mBinding.ivFactory.visibility = if (factory == "1") View.VISIBLE else View.GONE
            mBinding.tvFactory.visibility = if (factory == "1") View.VISIBLE else View.GONE
            mBinding.ivBrand.visibility = if (brand == "1") View.VISIBLE else View.GONE
            mBinding.tvBrand.visibility = if (brand == "1") View.VISIBLE else View.GONE
        } else {
            mBinding.llTag.visibility = View.GONE
        }
        mBinding.tvCpname.text = item.name
        item.merito?.setBname(sub = "", tvBname = mBinding.tvBname, length = 2)
        mBinding.tvAddress.text = "${item.province}${item.city}"
        val displayFac = item.displayFac
        if (!displayFac.isNullOrEmpty()) {
            val image = displayFac[0].image
            mBinding.ivFactoryPic.LoadSrc(image)
        }
        val goods = mutableListOf<SpuItemBean>()
        val spuList = item.spuList
        spuList?.let {
            val drop = it.take(2)
            goods.addAll(drop)
        }
        mBinding.rvImg.isNestedScrollingEnabled = false
        val adapter = FactoryImageAdapter(goods)
        mBinding.rvImg.adapter = adapter
        mBinding.rvImg.visibility = if (goods.isEmpty()) View.GONE else View.VISIBLE

        mBinding.clParent.setOnClickListener {
            onViewClickListener?.onClick(layoutPosition, item)
            ARouter.getInstance().build(ARoutUtils.CompanyInfoActivity).withString("cpyid", item.id)
                .navigation()
        }

    }

    var onViewClickListener: OnPositionTListener<CompanyItemBean>? = null


}